import React, { useEffect, useState } from 'react'
import { showProData } from '../../api/pro'
import { Table, Tag, Image, Switch } from 'antd'


export default function Index() {

    const [proList, setProList] = useState([])

    useEffect(() => {
        // 获取推荐的列表
        showProData({
            type: 'isrecommend',
            flag: 1
        }).then(res => {
            setProList(res.data)
        })
    }, [])


    const [current, setCurrent] = useState(1)

    const columns = [
        {
            title: '序号',
            render(t, r, i) {
                return <Tag>{(current - 1) * 10 + i + 1}</Tag>
            }
        }, {
            title: '商品名称',
            dataIndex: 'proname',
            width: 300
        }, {
            title: '商品图片',
            dataIndex: 'img1',
            render(t) {
                return <Image style={{ width: 80 }} src={t} />
            }
        }, {
            title: '商品分类',
            dataIndex: 'category',
        }, {
            title: '商品价格',
            dataIndex: 'originprice',
            sorter: (a, b) => a.originprice - b.originprice,
        }, {
            title: '商品折扣',
            dataIndex: 'discount',
            sorter: (a, b) => a.discount - b.discount,
        }, {
            title: '是否推荐',
            dataIndex: 'isrecommend',
            render(t) {
                return <Switch defaultChecked={t} />
            }
        }, {
            title: '是否售卖',
            dataIndex: 'issale',
            render(t, r) {
                return <Switch defaultChecked={t} />
            }
        }, {
            title: '是否秒杀',
            dataIndex: 'isseckill',
            render(t, r) {
                return <Switch defaultChecked={t} />
            }
        }
    ]

    return (
        <div>
            <h2>推荐列表</h2>
            <Table
                locale={{
                    triggerDesc: '点击降序',
                    triggerAsc: '点击升序',
                    cancelSort: '取消排序',
                    filterConfirm: '确定',
                    filterReset: '重置',
                }}
                scroll={{ y: 500 }}
                pagination={{
                    current,
                    onChange(page) {
                        setCurrent(page)
                    }
                }}
                rowKey={'proid'}
                dataSource={proList}
                columns={columns} />
        </div>
    )
}
